package com.education.platform.management.controller.user;

import com.education.platform.common.enums.OrderTypeEnum;
import com.education.platform.common.util.BeanUtil;
import com.education.platform.dao.entity.OrderReceiptDO;
import com.education.platform.dao.entity.UserOrderDO;
import com.education.platform.dao.model.UserOrderModel;
import com.education.platform.management.service.OrderReceiptService;
import com.education.platform.management.service.UserOrderService;
import com.education.platform.management.vo.request.OrderDeliverVO;
import com.education.platform.management.vo.request.OrderReturnVO;
import com.education.platform.management.vo.request.UserOrderQueryVO;
import com.education.platform.management.vo.response.UserOrderPageInfoVO;
import com.education.platform.management.vo.response.UserOrderResponseVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.bytebuddy.asm.Advice;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
* describe: 订单
* @author : tomato
* @date: 2020-06-29 10:28:08.867
**/
@RestController
@Api("订单")
@RequestMapping("userOrder")
@Validated
public class UserOrderController {

    private static final Logger LOGGER = LoggerFactory.getLogger(UserOrderController.class);

	@Autowired
    private UserOrderService userOrderService;

	@Autowired
    private OrderReceiptService orderReceiptService;
    /**
     * 分页查询
     * @param userOrderQueryVO 分页查询参数
     * @return 分页参数
     */
    @ApiOperation(value = "分页查询")
    @PostMapping("getPageList")
    public UserOrderPageInfoVO selectPageList(@RequestBody @Valid UserOrderQueryVO userOrderQueryVO) {
        String info = String.format("The method name[selectPageList] params:%s", userOrderQueryVO.toString());
        LOGGER.info(info);
        UserOrderModel userOrder = BeanUtil.convert(userOrderQueryVO, UserOrderModel.class);
        userOrder.setOrderType(OrderTypeEnum.OBJECT_ORDER.getValue());
        userOrder.setDeleted(false);
        Page<UserOrderResponseVO> page = PageHelper.startPage(userOrderQueryVO.getPageNum(), userOrderQueryVO.getPageSize());
        List<UserOrderModel> userOrderList = userOrderService.getOrderAndDetails(userOrder);
        UserOrderPageInfoVO userOrderPageInfo = new UserOrderPageInfoVO();
        BeanUtils.copyProperties(page.toPageInfo(), userOrderPageInfo);
        userOrderPageInfo.setPageNum(userOrderQueryVO.getPageNum());
        List<UserOrderResponseVO> voList = BeanUtil.convert(userOrderList, UserOrderResponseVO.class);
        userOrderPageInfo.setList(voList);

        return userOrderPageInfo;
    }

    @ApiOperation(value = "订单发货")
    @PostMapping("deliver")
    public void deliver(@RequestBody @Valid OrderDeliverVO deliverVO) {
        userOrderService.deliver(deliverVO);
    }



    @ApiOperation(value = "订单退货")
    @PostMapping("returnGoods")
    public void returnGoods(@RequestBody @Valid OrderReturnVO returnVO) {
        userOrderService.returnOrder(returnVO.getOrderId());
    }



}
